import { Observable } from "rxjs/Rx";
import { Subject } from "rxjs";
import ajax from "../../utils/axios";
import { myServer } from "../../urls/urls";
// const Mock = require("mockjs");
// const Random = Mock.Random;
// function getCategory() {
//   let categoryList = [];

//   for (let i = 0; i < 16; i++) {
//     let imgDataImg = Random.dataImage("45*45");
//     categoryList.push({
//       cid: Random.id(),
//       ctitle: Random.csentence(2, 4),
//       cpic: imgDataImg,
//       ctime: Random.date(),
//     });
//   }
//   return categoryList;
// }

// 类别
const cacheCategory = (() => {
  let response = null;
  return () => {
    let promise = new Promise((resolve, reject) => {
      if (response) {
        resolve(response);
      } else {
        ajax(`${myServer}index_category`).then((res) => {
          response = res;
          resolve(response);
        });
      }
    });
    return promise;
  };
})();
export const getCategory = cacheCategory;
// 3、根据经纬度获取商铺列表
const cacheShops = ((longitude, latitude) => {
  let response = null;
  return () => {
    let promise = new Promise((resolve, reject) => {
      if (response) {
        resolve(response);
      } else {
        ajax(`${myServer}shops`, { longitude, latitude }).then((res) => {
          response = res;
          resolve(response);
        });
      }
    });
    return promise;
  };
})();
export const reqShops = cacheShops;

// 商家列表
export const reqShopInfo = () => ajax("/info");
export const goods = () => ajax("/goods");
export const ratings = () => ajax("/ratings");

function getComments() {
  var subject = new Subject();
  var source = Observable.fromPromise(
    axios.get("http://cloud-music.pl-fe.cn/comment/music?id=186016&limit=1")
  );
  var multicasted = source.multicast(subject);
  multicasted.connect(subject);
  return multicasted;
}

function getHeroList() {
  Observable.fromPromise(
    axios.get(
      "https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2737105"
    )
  );
  return () => subject;
}
export { getComments, getHeroList };
